package com.mycase.im.boot.websocket.pattern.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mycase.im.boot.entity.communication.ClientMessage;
import com.mycase.im.boot.entity.communication.ResponseMessage;
import com.mycase.im.boot.websocket.WebSocketSignal;
import com.mycase.im.boot.websocket.pattern.MessageStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.Map;

/**
 * 用来解绑好友关系通知好友删除数据
 * @Author ZhuHJay
 * @Date 2021/10/21 20:29
 */
@Slf4j
@Service
public class RelieveFriendRelation implements MessageStrategy {

    @Override
    public Boolean isUse(int type) {
        return type == WebSocketSignal.MESSAGE_RELIEVE_FRIEND_RELATION;
    }

    @Override
    public void executeHandler(ClientMessage clientMessage,
                               ObjectMapper objectMapper,
                               Map<String, Session> clientMaps,
                               Session mySession) throws Exception {

        log.warn("解绑好友关系");
        // 查看被删除方是否在线
        if( clientMaps.containsKey(clientMessage.getToId()) ){
            sendMessage(objectMapper,
                    clientMaps.get(clientMessage.getToId()),
                    ResponseMessage.success()
                            .appendData("type", clientMessage.getType())
                            .appendData("friendId", clientMessage.getFromId())
            );
        }
    }
}
